package cn.edu.gench.learn.controller;

import cn.edu.gench.learn.model.City;
import cn.edu.gench.learn.model.District;
import cn.edu.gench.learn.model.Province;
import cn.edu.gench.learn.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Administrator on 2017/5/17.
 */
@Controller
@RequestMapping("/city")
public class CityController {

    @Autowired
    private CityService cityService;

    @RequestMapping("/show")
    public ModelAndView showCity(HttpServletRequest request){
        String pidS = request.getParameter("pid");
        String cidS  = request.getParameter("cid");

        List<Province> provinces =  cityService.getAll();
        int pid = 0;
        if(pidS == null){
            pid = provinces.get(0).getId();
        }else {
            pid = Integer.parseInt(pidS);
        }

        List<City> cities = cityService.getCityByPid(pid);

        int cid = 0;
        if(cidS == null){
            cid = cities.get(0).getId();
        }else {
            cid = Integer.parseInt(cidS);
        }

        List<District> districts = cityService.getDistrictByCid(cid);

        ModelAndView modelAndView   = new  ModelAndView("index");

        modelAndView.addObject("provinces",provinces);
        modelAndView.addObject("citys",cities);
        modelAndView.addObject("districts",districts);
        modelAndView.addObject("pid",pid);
        modelAndView.addObject("cid",cid);
        return modelAndView;
    }

}
